package cn.jhs.config.security.login.mobile_code;

import cn.jhs.config.security.sys.SysmangerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author Trump
 * @version 1.0.0
 * @ClassName UsernamePasswordAuthenticationProvider.java
 * @Description 手机验证码登录
 * @createTime 2019年08月15日 11:09:00
 */
public class MobileCodeAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
    protected Log logger = LogFactory.getLog(getClass());

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private SysmangerService sysmangerService;


    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        MobileCodeAuthenticationToken tokenReq = (MobileCodeAuthenticationToken) authentication;
        try {
            //校验验证码是否匹配
            if (!checkMobileCodeMatch(tokenReq)) {
                throw new BadCredentialsException("手机号码与验证码不匹配");
            }

            //todo
            //根据手机号码，获取登录账号

            String loginAcct = loadLoginAcctByMobile(tokenReq.getMobile());

            //todo
            //根据登录账号，查找登录人信息....
            UserDetails userDetails = userDetailsService.loadUserByUsername(loginAcct);
            return new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        } catch (BadCredentialsException e1) {
            logger.error(e1.getMessage());
            throw e1;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BadCredentialsException("手机验证码登录异常：" + e.getMessage());
        }

    }

    @Override
    protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        return null;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return (MobileCodeAuthenticationToken.class.isAssignableFrom(authentication));
    }

    /**
     * @param tokenReq
     * @throws
     * @title 根据手机号码+输入的验证码 查询验证是否匹配
     * @description
     * @author Trump
     * @updateTime 2019-08-15 13:52
     */
    protected boolean checkMobileCodeMatch(MobileCodeAuthenticationToken tokenReq) {
        //todo 根据手机号码，查询验证码 并与输入的验证码做匹配
        //todo 1.根据手机号码 查询发送的验证码
        String code = sysmangerService.getMobileLoginCode(tokenReq.getMobile());

        //todo 2.根据返回的验证码 与 输入的验证码 做匹配
        return tokenReq.getCode().equals(code);
    }


    /**
     * @param mobile 手机号码
     * @throws
     * @title 根据手机号码 获取 用户名
     * @description
     * @author Trump
     * @updateTime 2019-08-15 13:52
     */
    protected String loadLoginAcctByMobile(String mobile) {
        return sysmangerService.getLoginAcctByMobile(mobile);
    }

}
